package org.serverct.finalfight.game.gui;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.serverct.finalfight.FinalFight;
import org.serverct.finalfight.game.listener.gui.SpectatorGuiListener;
import org.serverct.finalfight.internal.game.basic.BasicGame;

import java.util.Arrays;
import java.util.UUID;

public class SpectatorTeleporter {
    public static ItemStack menuItem = generateItem(Material.COMPARATOR, "§b旁观者传送装置", "§7- 点击此物品传送到其他玩家身边观战");

    public static int[] slots = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 17, 18, 26, 27, 35, 36, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53};

    public static void initialize() {
        Bukkit.getPluginManager().registerEvents(new SpectatorGuiListener(), FinalFight.plugin);
    }

    public static Inventory generateInventory(BasicGame game) {
        if (game == null) {
            return null;
        }
        Inventory inv = Bukkit.createInventory(new SpectatorGuiHolder(), 54, "传送到玩家");
        for (int i : slots) {
            inv.setItem(i, generateItem(Material.GRAY_STAINED_GLASS_PANE, "§7 "));
        }
        for (UUID uuid : game.players.keySet()) {
            Player player = Bukkit.getPlayer(uuid);
            ItemStack item = generateItem(Material.PLAYER_HEAD, (game.isAlive(player) ? "§a✔存活: §e" : "§c✖死亡: §e") + player.getName());
            SkullMeta meta = (SkullMeta) item.getItemMeta();
            meta.setPlayerProfile(player.getPlayerProfile());
            item.setItemMeta(meta);
            inv.addItem(item);
        }
        return inv;
    }

    public static ItemStack generateItem(Material type, String name, String... lore) {
        ItemStack item = new ItemStack(type);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(name);
        meta.setLore(Arrays.asList(lore));
        item.setItemMeta(meta);
        return item;
    }

    public static class SpectatorGuiHolder implements InventoryHolder {
        @Override
        public Inventory getInventory() {
            return null;
        }
    }
}
